package com.sneaker.shower.controller;

import com.sneaker.shower.common.BaseView;
import com.sneaker.shower.entity.dto.SysUserBindDTO;
import com.sneaker.shower.entity.dto.SysUserDTO;
import com.sneaker.shower.entity.dto.UserDTO;
import com.sneaker.shower.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api
@RestController
public class SysUserController {
    private final SysUserService sysUserService;

    public SysUserController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @RequestMapping(value = "/sysUser/invitationCode", method = RequestMethod.GET)
    public BaseView<String> invitationCode() {
        return new BaseView<>(sysUserService.invitationCode());
    }

    @RequestMapping(value = "/sysUser/addSysUser", method = RequestMethod.POST)
    @ApiOperation("客服登录并绑定，需传入客服所要绑定的门店guid")
    public BaseView<String> addSysUser(@Validated(SysUserDTO.Add.class) @RequestBody SysUserDTO dto) {
        return new BaseView<>(sysUserService.addSysUser(dto));
    }

    @RequestMapping(value = "/sysUser/modifySysUser", method = RequestMethod.POST)
    public BaseView<Boolean> modifySysUser(@RequestBody SysUserDTO dto) {
        return new BaseView<>(sysUserService.modifySysUser(dto));
    }

    @RequestMapping(value = "/sysUser/bind", method = RequestMethod.POST)
    public BaseView<Boolean> bind(@Validated @RequestBody SysUserBindDTO sysUserBindDTO) {
        SysUserDTO sysUserDTO = new SysUserDTO();
        sysUserDTO.setGuid(sysUserBindDTO.getSysUserGuid().toString());
        sysUserDTO.setOpenId(sysUserBindDTO.getOpenId());
        return new BaseView<>(sysUserService.modifySysUser(sysUserDTO));
    }

    @RequestMapping(value = "/sysUser/delSysUser", method = RequestMethod.DELETE)
    public BaseView<Boolean> delSysUser(String userName, String guid) {
        return new BaseView<>(sysUserService.delSysUser(userName, guid));
    }

    @RequestMapping(value = "/sysUser/getNumber", method = RequestMethod.GET)
    public BaseView<Integer> getNumber(Long organizationGuid) {
        return new BaseView<>(sysUserService.getNumber(organizationGuid));
    }

    @RequestMapping(value = "/sysUser/list", method = RequestMethod.GET)
    public BaseView<List<UserDTO>> listAll() {
        return new BaseView<>(sysUserService.listAll());
    }

    @RequestMapping(value = "/sysUser/login", method = RequestMethod.GET)
    @ApiOperation("账号密码登录")
    public BaseView<UserDTO> login(String userName, String password) {
        return new BaseView<>(sysUserService.login(userName, password));
    }

    @RequestMapping(value = "/sysUser/login/openId", method = RequestMethod.GET)
    @ApiOperation("客服登录，传入OpenGuid登录")
    public BaseView<UserDTO> login(String openId) {
        return new BaseView<>(sysUserService.login(openId));
    }
}
